////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.users.display {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.MouseEvent;
	
	import kiwi.components.DragObject;
	import kiwi.components.IDropTarget;
	import kiwi.events.CallbackEvent;
	import kiwi.utils.Validation;
	
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	import plantool.modules.users.data.UserHourData;
	import plantool.modules.users.data.UserHourTypes;
	import plantool.modules.workplaces.display.IWorkplaceDay;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class UserHour extends UserDisplayObject implements IUserHour {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _color:uint				= 0;
		private var _date:Date				= null;
		private var _hotspot:DragObject		= null;
		private var _hourData:UserHourData 	= null;
		private var _skin:UserHourSkin		= new UserHourSkin();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get date():Date {
			return _date;
		}
		public function set date(value:Date):void {
			_date = new Date(value);
		}
		
		public function get hourData():UserHourData {
			return _hourData;
		}
		public function set hourData(value:UserHourData):void {
			//do nothing if nothing changes
			if (_hourData == value) return;
			
			//store hour data
			_hourData = value;
			
			//update skin color
			updateSkin();
			
			//redraw button
			invalidate();
		}

		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function UserHour() {
			super();
		}
		
		public override function dispose():void {
			//dispose date
			_date = null;
			
			//dispose hourdata
			_hourData = null;
			
			//dispose hotspot
			if (Validation.isSet(_hotspot)) {
				//dispose hotspot
				_hotspot.dispose();
				_hotspot = null;
			}
			
			if (Validation.isSet(_skin)) {
				_skin = null;
			}
			
			//dispose object
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onComplete_createHour(result:QueryResult):void {
		}
		
		private function onDrop_hour(target:IDropTarget, item:DragObject):void {
			//validate target
			if (target is IWorkplaceDay) {
				//get workplace id
				var targetid:uint = (target as IWorkplaceDay).workplace.id;
				
				//create till-date
				var till:Date 	= new Date(_date);
				till.hours		+= 1;
				userApi.saveUserHour(user, _date, till, UserHourTypes.USER_WORKPLACE, targetid,  onComplete_createHour, onError_createHour);
			}
		}
		
		private function onValidateDrop_hour(target:IDropTarget, item:DragObject):Boolean {
			if (target is IWorkplaceDay) {
				//cast day
				var workplaceDay:IWorkplaceDay = target as IWorkplaceDay;
				
				//validate day
				return dateApi.validateDay(date, workplaceDay.date);
			}
			
			return false;
		}
		
		private function onError_createHour(result:QueryErrorResult):void {
			trace("Error", result.query);
		}
		
		private function onDblClick_spot(e:CallbackEvent):void {
			if (e.userData.type == MouseEvent.DOUBLE_CLICK) {
				if (!Validation.isSet(_hourData)) {
					//create new free hour
					var till:Date 	= new Date(_date);
					till.hours		+= 1;
					userApi.saveUserHour(user, _date, till, UserHourTypes.USER_FREE, 0, onComplete_createHour, onError_createHour);
				} else {
					//validate free hour before we remove
					if (_hourData.type == UserHourTypes.USER_FREE) {
						userApi.removeUserHours([_hourData], onComplete_createHour, onError_createHour);
					}
				}
			}
		}
		
		private function updateSkin():void {
			//set color according to user-hours
			if (Validation.isSet(_hourData)) {
				switch(_hourData.type) {
					//set color according to user-hours
					case UserHourTypes.USER_FREE:
						_skin.color = 0;
						break;
					default:
						_skin.color = 0xFFFFFF;
						break;
				}
			} else {
				_skin.color = user.color;
			}
		}
		
		protected override function init():void {
			//create hotspot
			_hotspot			= new DragObject();
			_hotspot.skin		= _skin;
			_hotspot.width 		= 20;
			_hotspot.height		= 20;
			addChild(_hotspot);
			
			//drag properties
			_hotspot.dragItem				= this;
			_hotspot.droppedHandler 		= onDrop_hour;
			_hotspot.validateDropHandler 	= onValidateDrop_hour;
			_hotspot.dropTargets.push(IWorkplaceDay);
			
			//create dbl-click for creating free hours
			_hotspot.doubleClickEnabled	= true;
			_hotspot.mouseHandler		= onDblClick_spot;
			
			//update skin
			updateSkin();
			
			//draw hour
			invalidate();
		}
		
		public override function draw():void {
			//validate dragging
			if (Validation.isSet(_hotspot)) {
				_hotspot.dragEnabled = !Validation.isSet(_hourData);
			}
		}
	}
}